package com.basic.library.view;


import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

/**
 * @author yj
 * @description
 */
public class MyTipCenterEditText extends AppCompatEditText {
    public MyTipCenterEditText(@NonNull Context context) {
        this(context, null);
    }

    public MyTipCenterEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public MyTipCenterEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (getText().toString().isEmpty())
                    setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);
                else
                    setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
            }
        });
    }

}
